/*
 * Copyright (c) 2014, Mentor Graphics Corporation
 * Copyright (c) 2015 Xilinx, Inc.
 * Copyright (c) 2016 Freescale Semiconductor, Inc.
 * Copyright 2016-2022 NXP
 * Copyright 2021 ACRIOS Systems s.r.o.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef RPMSG_LITE_H_
#define RPMSG_LITE_H_

#if defined(__cplusplus)
extern "C" {
#endif

#include <stddef.h>
#include "rpmsg_compiler.h"
#include "virtqueue.h"
#include "rpmsg_env.h"
#include "llist.h"
#include "rpmsg_default_config.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/
/*!< RPMsg-Lite版本 */
#define RL_VERSION          "5.0.0"

/* 共享内存“分配器”参数 */
#define RL_WORD_SIZE (sizeof(uint32_t))
#define RL_WORD_ALIGN_UP(a) \
    (((((uint32_t)(a)) & (RL_WORD_SIZE - 1U)) != 0U) ? \
     ((((uint32_t)(a)) & (~(RL_WORD_SIZE - 1U))) + 4U) : ((uint32_t)(a)))
#define RL_WORD_ALIGN_DOWN(a) \
    (((((uint32_t)(a)) & (RL_WORD_SIZE - 1U)) != 0U) ? \
     (((uint32_t)(a)) & (~(RL_WORD_SIZE - 1U))) : ((uint32_t)(a)))

/* 设备类型, 空指针等的定义. */
#define RL_SUCCESS          (0)
#define RL_NULL             ((void *)0)
#define RL_REMOTE           (0)
#define RL_MASTER           (1)
#define RL_TRUE             (1UL)
#define RL_FALSE            (0UL)
#define RL_ADDR_ANY         (0xFFFFFFFFU)
#define RL_RELEASE          (0)
#define RL_HOLD             (1)
#define RL_DONT_BLOCK       (0)
#define RL_BLOCK            (0xFFFFFFFFU)

/* 错误编码. */
#define RL_ERRORS_BASE      (-5000)
#define RL_ERR_NO_MEM       (RL_ERRORS_BASE - 1)
#define RL_ERR_BUFF_SIZE    (RL_ERRORS_BASE - 2)
#define RL_ERR_PARAM        (RL_ERRORS_BASE - 3)
#define RL_ERR_DEV_ID       (RL_ERRORS_BASE - 4)
#define RL_ERR_MAX_VQ       (RL_ERRORS_BASE - 5)
#define RL_ERR_NO_BUFF      (RL_ERRORS_BASE - 6)
#define RL_NOT_READY        (RL_ERRORS_BASE - 7)
#define RL_ALREADY_DONE     (RL_ERRORS_BASE - 8)

/* 初始化标志. */
#define RL_NO_FLAGS         (0U)

/* 接收回调函数类型. */
typedef int32_t (*rl_ept_rx_cb_t)(void *payload, uint32_t payload_len, \
                                  uint32_t src, void *priv);

/* RPMsg-Lite端点结构. */
struct rpmsg_lite_endpoint {
    uint32_t addr;        /*!< 端点地址 */
    rl_ept_rx_cb_t rx_cb; /*!< ISR回调函数 */
    void *rx_cb_data;     /*!< ISR回调函数参数 */
    void *rfu;            /*!< 预留, 未使用 */
    /* 在32位架构上16字节对齐 */
};

/* RPMsg-Lite端点静态上下文结构. */
struct rpmsg_lite_ept_static_context {
    struct rpmsg_lite_endpoint ept; /*!< RPMsg-Lite端点 */
    struct llist node;              /*!< 链表节点 */
};

/* 描述RPMsg-Lite通信堆栈的本地实例, 并保存堆栈内部所需的所有运行时变量的结构. */
struct rpmsg_lite_instance {
    struct virtqueue *rvq;      /*!< 接收虚拟队列 */
    struct virtqueue *tvq;      /*!< 发送虚拟队列 */
    struct llist *rl_endpoints; /*!< 端点的链表 */
    LOCK *lock;                 /*!< 本地RPMsg Lite互斥锁 */
#if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
    LOCK_STATIC_CONTEXT lock_static_ctxt; /*!< 用于创建锁对象的静态上下文 */
#endif
    uint32_t link_state;        /*!< 链接的状态, 连接或断开 */
    char *sh_mem_base;          /*!< 共享内存的基址 */
    uint32_t sh_mem_remaining;  /*!< 共享内存中剩余未使用缓冲区的数量 */
    uint32_t sh_mem_total;      /*!< 共享内存中的缓冲区总数 */
    struct virtqueue_ops const *vq_ops; /*!< 操作函数表指针 */
#if defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
    void *env;                  /*!< 指向环境层上下文的指针 */
#endif

#if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
    struct vq_static_context vq_ctxt[2];
#endif
    uint32_t link_id;           /*!< RPMsg-Lite实例的链接ID */
};

/*******************************************************************************
 * API
 ******************************************************************************/

/* 导出API函数 */

/**
 * @brief  RPMsg-Lite通信堆栈初始化. 必须在其他RPMSG Lite API之前调用. 由主机调用.
 * @param  shmem_addr RPMsg-Lite实例使用的共享内存基址.
 * @param  shmem_length RPMsg-Lite实例使用的共享内存区域大小.
 * @param  link_id 用于定义RPMsg-Lite实例的链接ID, 参见rpmsg_platform.h
 * @param  init_flags 初始化标志.
 * @param  env_cfg 指向RPMsg-Lite环境层的初始化数据的指针,
 *         当环境层使用它自己的上下文时使用(RL_USE_ENVIRONMENT_CONTEXT).
 * @param  static_context 指向预分配的RPMsg-Lite上下文的指针,
 *         用于静态API (RL_USE_STATIC_API)
 * @return 返回新的RPMsg-Lite实例的指针或RL_NULL.
 */
#if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
struct rpmsg_lite_instance *rpmsg_lite_master_init(void *shmem_addr,
    size_t shmem_length, uint32_t link_id, uint32_t init_flags,
    struct rpmsg_lite_instance *static_context);
#elif defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
struct rpmsg_lite_instance *rpmsg_lite_master_init(void *shmem_addr,
    size_t shmem_length, uint32_t link_id, uint32_t init_flags, void *env_cfg);
#else
struct rpmsg_lite_instance *rpmsg_lite_master_init(void *shmem_addr,
    size_t shmem_length, uint32_t link_id, uint32_t init_flags);
#endif

/**
 * @brief  RPMsg-Lite通信堆栈初始化. 必须在其他RPMSG Lite API之前调用. 由远程调用.
 * @param  shmem_addr RPMsg-Lite实例使用的共享内存基址.
 * @param  link_id 用于定义RPMsg-Lite实例的链接ID, 参见rpmsg_platform.h
 * @param  init_flags 初始化标志.
 * @param  env_cfg 指向RPMsg-Lite环境层的初始化数据的指针,
 *         当环境层使用它自己的上下文时使用(RL_USE_ENVIRONMENT_CONTEXT).
 * @param  static_context 指向预分配的RPMsg-Lite上下文的指针,
 *         用于静态API (RL_USE_STATIC_API)
 * @return 返回新的RPMsg-Lite实例的指针或RL_NULL.
 */
#if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
struct rpmsg_lite_instance *rpmsg_lite_remote_init(void *shmem_addr,
    uint32_t link_id, uint32_t init_flags,
    struct rpmsg_lite_instance *static_context);
#elif defined(RL_USE_ENVIRONMENT_CONTEXT) && (RL_USE_ENVIRONMENT_CONTEXT == 1)
struct rpmsg_lite_instance *rpmsg_lite_remote_init(void *shmem_addr,
    uint32_t link_id, uint32_t init_flags, void *env_cfg);
#else
struct rpmsg_lite_instance *rpmsg_lite_remote_init(void *shmem_addr,
    uint32_t link_id, uint32_t init_flags);
#endif

/**
 * @brief  RPMsg-Lite通信栈反初始化. 该函数总是返回成功.
 * @param  rpmsg_lite_dev 指向RPMsg-Lite实例指针.
 * @return 返回该函数执行状态, 成功时返回RL_SUCCESS.
 */
int32_t rpmsg_lite_deinit(struct rpmsg_lite_instance *rpmsg_lite_dev);

/**
 * @brief  创建一个新的rpmsg端点, 用于通信.
 * @param  rpmsg_lite_dev 指向RPMsg-Lite实例指针.
 * @param  addr 所需地址, RL_ADDR_ANY用于自动分配.
 * @param  rx_cb 接收回调函数.
 * @param  rx_cb_data 接收回调函数的参数, 传递给rx_cb.
 * @param  ept_context 指向预分配的端点上下文的指针, 用于静态API (RL_USE_STATIC_API).
 * @return 成功时返回新的端点的指针, 失败返回RL_NULL.
 */
#if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
struct rpmsg_lite_endpoint *rpmsg_lite_create_ept(
    struct rpmsg_lite_instance *rpmsg_lite_dev,
    uint32_t addr, rl_ept_rx_cb_t rx_cb, void *rx_cb_data,
    struct rpmsg_lite_ept_static_context *ept_context);
#else
struct rpmsg_lite_endpoint *rpmsg_lite_create_ept(
    struct rpmsg_lite_instance *rpmsg_lite_dev,
    uint32_t addr, rl_ept_rx_cb_t rx_cb, void *rx_cb_data);
#endif

/**
 * @brief  该函数删除rpmsg端点并执行清理.
 * @param  rpmsg_lite_dev 指向RPMsg-Lite实例指针.
 * @param  rl_ept 指向要销毁的端点的指针.
 * @return
 */
int32_t rpmsg_lite_destroy_ept(struct rpmsg_lite_instance *rpmsg_lite_dev,
    struct rpmsg_lite_endpoint *rl_ept);

/**
 * @brief  将长度为size的data字段中的消息发送到地址为dst的远程端点.
 *         ept->addr作为发送消息的rpmsg头中的源地址.
 * @param  rpmsg_lite_dev 指向RPMsg-Lite实例指针.
 * @param  ept 指向发送端端点指针.
 * @param  dst 目的端点地址.
 * @param  data 有效载荷的缓冲区.
 * @param  size 有效载荷长度, 单位: 字节.
 * @param  timeout 超时时间, 单位: 毫秒. 非阻塞为0.
 * @return 返回该函数执行状态, 成功时返回RL_SUCCESS.
 */
int32_t rpmsg_lite_send(struct rpmsg_lite_instance *rpmsg_lite_dev,
    struct rpmsg_lite_endpoint *ept, uint32_t dst, char *data,
    uint32_t size, uint32_t timeout);

/**
 * @brief  函数来获取链路状态.
 * @param  rpmsg_lite_dev 指向RPMsg-Lite实例指针.
 * @return 连接时返回RL_TRUE, 断开时返回RL_FALSE.
 */
uint32_t rpmsg_lite_is_link_up(struct rpmsg_lite_instance *rpmsg_lite_dev);

/**
 * @brief  函数等待直到建立连接. 一旦设置了连接状态, 返回RL_TRUE, 超时返回RL_FALSE.
 * @param  rpmsg_lite_dev 指向RPMsg-Lite实例指针.
 * @param  timeout 超时时间, 单位: 毫秒. 非阻塞为0.
 * @return 连接时返回RL_TRUE, 超时时返回RL_FALSE.
 *
 */
uint32_t rpmsg_lite_wait_for_link_up(struct rpmsg_lite_instance *rpmsg_lite_dev,
    uint32_t timeout);

#if defined(RL_API_HAS_ZEROCOPY) && (RL_API_HAS_ZEROCOPY == 1)

/**
 * @brief  释放发送缓冲区, 以便将来在vring中重用. 该函数可以在处理完接收缓冲区中的消息后调用.
 * @param  rpmsg_lite_dev 指向RPMsg-Lite实例指针.
 * @param  rxbuf 带有消息有效负载的接收缓冲区.
 * @return 返回该函数执行状态, 成功时返回RL_SUCCESS.
 */
int32_t rpmsg_lite_release_rx_buffer(struct rpmsg_lite_instance *rpmsg_lite_dev,
    void *rxbuf);

/**
 * @brief  为消息有效负载分配发送缓冲区. 该函数在进程上下文中调用, 以获取vring中的发送缓冲区.
 *         通过这种方式, 应用程序可以直接将其消息放入vring发送缓冲区, 而不需要从应用程序缓冲
 *         区复制. 应用程序负责用数据正确地填充已分配的发送缓冲区, 并将正确的参数传递给
 *         rpmsg_lite_send_nocopy()函数以执行数据不复制发送机制.
 * @param  rpmsg_lite_dev 指向RPMsg-Lite实例指针.
 * @param  size 指向用于存储可用的最大有效负载大小的指针.
 * @param  timeout 超时时间, 单位: 毫秒. 非阻塞为0.
 * @return 成功时返回发送缓冲区地址, 失败时返回RL_NULL.
 * @see    rpmsg_lite_send_nocopy
 */
void *rpmsg_lite_alloc_tx_buffer(struct rpmsg_lite_instance *rpmsg_lite_dev,
    uint32_t *size, uint32_t timeout);

/**
 * @brief  使用rpmsg_lite_alloc_tx_buffer()分配的发送缓冲区发送消息. 该函数将长度为len的
 *         txbuf发送到远端dst地址, 并使用ept->addr作为源地址.
 *         应用程序必须遵循以下规则:
 *          1. 使用rpmsg_lite_alloc_tx_buffer()分配发送缓冲区分配.
 *          2. 将数据填充到预分配的发送缓冲区中.
 *          3. 填充数据不超过缓冲区大小.
 *          4. 数据缓存一致性.
 *         在rpmsg_lite_send_nocopy()函数发出后, 发送缓冲区不再属于发送任务, 除非
 *         rpmsg_lite_send_nocopy()函数失败并返回错误, 否则不能再碰它.
 * @param  rpmsg_lite_dev 指向RPMsg-Lite实例指针.
 * @param  ept 指向发送端端点指针.
 * @param  dst 目的端点地址.
 * @param  data 已填充消息的发送缓冲区.
 * @param  size 有效载荷长度, 单位: 字节.
 * @return 成功时返回0, 失败时返回错误码.
 * @see    rpmsg_lite_alloc_tx_buffer
 */
int32_t rpmsg_lite_send_nocopy(struct rpmsg_lite_instance *rpmsg_lite_dev,
    struct rpmsg_lite_endpoint *ept, uint32_t dst, void *data, uint32_t size);

#endif /* RL_API_HAS_ZEROCOPY */

#if defined(__cplusplus)
}
#endif

#endif /* RPMSG_LITE_H_ */
